import 'dart:ui';

import 'package:dio/dio.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/ui/custom/LoadingDialog.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';

import 'model/set_pwd_entity.dart';

///************************
/// @function 修改密码
/// @创建日期 2019-10-28 11:05:21
///************************
class ChangePwd extends StatefulWidget {
  @override
  _ChangePwdState createState() => _ChangePwdState();
}

class _ChangePwdState extends State<ChangePwd> {
  int line1Color = 0xFFe5e5e5;
  int line2Color = 0xFFe5e5e5;
  int line3Color = 0xFFe5e5e5;

  FocusNode _focusNode1, _focusNode2, _focusNode3;
  TextEditingController _t1Controller = TextEditingController();
  TextEditingController _t2Controller = TextEditingController();
  TextEditingController _t3Controller = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _focusNode1 = FocusNode();
    _focusNode1.addListener(() {
      if (_focusNode1.hasFocus) {
        print("name 获取到焦点");
        setState(() {
          line1Color = 0xFFdf3033;
          line2Color = 0xFFe5e5e5;
          line3Color = 0xFFe5e5e5;
        });
      }
    });
    _focusNode2 = FocusNode();

    _focusNode2.addListener(() {
      if (_focusNode2.hasFocus) {
        print("pwd 获取到焦点");
        setState(() {
          line2Color = 0xFFdf3033;
          line1Color = 0xFFe5e5e5;
          line3Color = 0xFFe5e5e5;
        });
      }
    });
    _focusNode3 = FocusNode();
    _focusNode3.addListener(() {
      if (_focusNode3.hasFocus) {
        print("pwd 获取到焦点");
        setState(() {
          line3Color = 0xFFdf3033;
          line1Color = 0xFFe5e5e5;
          line2Color = 0xFFe5e5e5;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ListView(
        children: <Widget>[
          ///x
          getRow1(),

          ///修改密码
          getRow2(),

          ///输入原始密码
          getRow4(),
          getLine1(),

          ///输入新密码密码
          getRow5(),
          getLine2(),

          ///再次输入
          getRow6(),
          getLine3(),
          getButton(),
        ],
      ),
    );
  }

  getRow1() {
    return Container(
      alignment: Alignment.centerLeft,
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(30),
          MediaQueryData.fromWindow(window).padding.top, 0, 0),
      height: ScreenUtil().setWidth(88),
      child: GestureDetector(
        onTap: () {
          Application.router.pop(context);
        },
        child: Image.asset(
          Utils.getImgPath("icon_delete"),
          width: ScreenUtil().setWidth(32),
          height: ScreenUtil().setWidth(32),
        ),
      ),
    );
  }

  getRow2() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(92), ScreenUtil().setWidth(88), 0, 0),
      alignment: Alignment.centerLeft,
      child: Text(
        "修改密码",
        style: TextStyle(
          fontSize: ScreenUtil().setWidth(48),
          color: Colours.appC1,
          fontFamily: Font.PingFangBlod,
        ),
        /*TextStyles.custom(48, Colours.appC1, AppFontFamily.PingFangBlod)*/
      ),
    );
  }

  getRow4() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(90),
          ScreenUtil().setWidth(90), ScreenUtil().setWidth(90), 0),
      child: TextField(
        controller: _t1Controller,
        obscureText: true,
        focusNode: _focusNode1,
        decoration: InputDecoration(
          hintText: "请输入原密码",
          hintStyle: TextStyles.custom(
              28, Color(0xFF999999), Font.PingFangMedium),
          border: InputBorder.none,
        ),
      ),
    );
  }

  getRow5() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(90),
          ScreenUtil().setWidth(20), ScreenUtil().setWidth(90), 0),
      child: TextField(
        controller: _t2Controller,
        obscureText: true,
        focusNode: _focusNode2,
        decoration: InputDecoration(
          hintText: "请输入6-20位新密码",
          hintStyle: TextStyles.custom(
              28, Color(0xFF999999), Font.PingFangMedium),
          border: InputBorder.none,
        ),
      ),
    );
  }

  getRow6() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(90),
          ScreenUtil().setWidth(20), ScreenUtil().setWidth(90), 0),
      child: TextField(
        controller: _t3Controller,
        obscureText: true,
        focusNode: _focusNode3,
        decoration: InputDecoration(
          hintText: "请再次输入",
          hintStyle: TextStyles.custom(
              28, Color(0xFF999999), Font.PingFangMedium),
          border: InputBorder.none,
        ),
      ),
    );
  }

  getLine1() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(90), 0, ScreenUtil().setWidth(90), 0),
      width: ScreenUtil().setWidth(750),
      color: Color(line1Color),
      height: ScreenUtil().setWidth(1),
    );
  }

  getLine2() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(90), 0, ScreenUtil().setWidth(90), 0),
      width: ScreenUtil().setWidth(750),
      color: Color(line2Color),
      height: ScreenUtil().setWidth(1),
    );
  }

  getLine3() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(90), 0, ScreenUtil().setWidth(90), 0),
      width: ScreenUtil().setWidth(750),
      color: Color(line3Color),
      height: ScreenUtil().setWidth(1),
    );
  }

  getButton() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(56),
          ScreenUtil().setWidth(96), ScreenUtil().setWidth(56), 0),
      child: UiUtils.bottomBotton("确定", () {
        if (checkPwd()) {
          showDialog(
              context: context,
              barrierDismissible: false,
              builder: (BuildContext context) {
                return new LoadingDialog(
                  text: "正在设置密码…",
                );
              });

          _setPwd();
        }
      }),
    );
  }

  Future _setPwd() async {
    FormData params = FormData.from(
        {"newPwd": _t2Controller.text, "oldPwd": _t1Controller.text});
    DioManager.getInstance().post(Api.SETPWD, params, (json) {
      SetPwdEntity entity = SetPwdEntity.fromJson(json);
      if (entity.code == Api.SUCCESS_CODE) {
        ToastUtil.showShortToast("修改成功");
        Navigator.pop(context); //退出dialog
        Navigator.pop(context); //退出此页面
        return;
      }

      if (entity.code == Api.ERROR_CODE || entity.code == Api.ERROR_CODE_0) {
        Navigator.pop(context);
        ToastUtil.showShortErrToast(entity.message);
        return;
      }
      Navigator.pop(context);
      ToastUtil.showShortErrToast(Api.ERR_NOTHING);
    },
        //错误回调
        (error) {
      ToastUtil.showShortErrToast(Api.ERR_TIME_OUT);
      Navigator.pop(context);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _t1Controller.dispose();
    _t2Controller.dispose();
    _t3Controller.dispose();
  }

  bool checkPwd() {
    String p1 = _t1Controller.text;
    String p2 = _t2Controller.text;
    String p3 = _t3Controller.text;

    if (p1.isEmpty) {
      ToastUtil.showShortErrToast("原密码不能为空");
      return false;
    }
    if (p2.isEmpty) {
      ToastUtil.showShortErrToast("新密码不能为空");
      return false;
    }
    if (p2.length < 6) {
      ToastUtil.showShortErrToast("密码长度不低于6位");
      return false;
    }
    if (p2.length > 20) {
      ToastUtil.showShortErrToast("密码长度不高于20位");
      return false;
    }

    if (p3 != p2) {
      ToastUtil.showShortErrToast("两次输入的密码一致");
      return false;
    }

    return true;
  }
}
